/**
 * Copyright (c) 2010 BullionXpress.com,
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
package com.bullionxpress.server.impl;

import java.util.logging.Level;
import java.util.logging.Logger;
import com.bullionxpress.shared.service.AccountNotFoundException;
import com.bullionxpress.shared.service.InvalidPasswordException;
import com.bullionxpress.shared.service.UserNotFoundException;
import com.bullionxpress.shared.service.UserDatabase;
import com.bullionxpress.shared.model.User;
import java.rmi.Remote;
import java.rmi.RemoteException;
import com.bullionxpress.shared.model.Account;
import com.bullionxpress.shared.model.Position;
import com.bullionxpress.shared.service.AccountDatabase;
import com.bullionxpress.shared.service.ForgotPasswordService;
import com.bullionxpress.shared.service.ChangePasswordService;
import com.bullionxpress.shared.service.LoginService;
import com.bullionxpress.shared.service.PositionDatabase;
import com.bullionxpress.shared.service.PositionNotFoundException;
import com.bullionxpress.shared.util.Log;

/**
 *
 * @author BullionXpress.com
 */
public class UserManager extends java.rmi.server.UnicastRemoteObject implements LoginService, ForgotPasswordService, ChangePasswordService {

    //UserDatabase db = null;
    public UserManager() throws RemoteException
    {
        //db.connect();
        //connect();
    }

    

    public User login(String userid, String password) throws InvalidPasswordException, UserNotFoundException {
        {
            try {
                if (userid == null) {
                    throw new IllegalArgumentException("Username cannot be null");
                }
                if (password == null) {
                    throw new IllegalArgumentException("Password cannot be null");
                }
                userid = userid.toLowerCase();
                UserDatabase db = (UserDatabase) new UserRegistrar();
                User u = db.get(userid);
                Account account = null;
                AccountDatabase adb = new AccountRegistrar();
                account = adb.getAccountByOwner(u.getUserID());
                PositionDatabase pdb = new PositionManager();
                Position[] list = pdb.getPositionListByAccount(account.getId());

                if (account == null) {
                    throw new IllegalStateException("Every user needs to have a valid account");
                }
                account.setPositions(list);
                u.setAccount(account);
                if (u == null) {
                    throw new UserNotFoundException();
                }
                String pwd = u.getPassword();
                if (pwd == null) {
                    throw new IllegalStateException("User password cannot be null");
                }
                if (pwd.equals(password)) {
                    return u;
                } else {
                    throw new InvalidPasswordException("Incorrect password");
                }
            } catch(PositionNotFoundException pex){
                return null;
            } catch (AccountNotFoundException ex) {
                Log.info("Problem logging in");
                Logger.getLogger(UserManager.class.getName()).log(Level.SEVERE, null, ex);
                return null;
            } catch (RemoteException ex) {
                Logger.getLogger(UserManager.class.getName()).log(Level.SEVERE, null, ex);
                return null;
            }
        }

    }

    public void logout(User user) throws UserNotFoundException {
        try {
            //throw new UnsupportedOperationException("Not supported yet.");
            if (user == null) {
                throw new IllegalArgumentException("user cannot be null");
            }
            UserDatabase db = (UserDatabase) new UserRegistrar();
            User u = db.get(user.getUserID());
            if (u == null) {
                throw new UserNotFoundException();
            }
        } catch (RemoteException ex) {
            Logger.getLogger(UserManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void remind(String userid) throws UserNotFoundException {
        try {
            UserDatabase db = (UserDatabase) new UserRegistrar();
            User u = db.get(userid);
            if (u == null) {
                throw new UserNotFoundException();
            }
            // send email
        } catch (RemoteException ex) {
            Logger.getLogger(UserManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        // send email
    }

    public void change(String userid, String password, String old) throws InvalidPasswordException, UserNotFoundException {
        try {
            UserDatabase db = (UserDatabase) new UserRegistrar();
            User u = db.get(userid);
            if (u == null) {
                throw new UserNotFoundException();
            }
            if (!old.equals(u.getPassword())) {
                throw new InvalidPasswordException();
            }
            try {
                u.setPassword(password);
                db.save(u);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } catch(RemoteException ex){
            Logger.getLogger(UserManager.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
